package com.xiaotu.spider.pipeline;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVPubCommentModel;
import com.xiaotu.common.model.TVRecordModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.SupplementService;
import com.xiaotu.spider.APipeline;

@Component("SupplementPipeline")
public class SupplementPipeline extends APipeline{
	
	@Autowired
	private SupplementService supplementService;
	
	private JSONObject spiderInfo;
	
	
	public SupplementPipeline(JSONObject spiderInfo){
		this.spiderInfo=spiderInfo;
	}
	
	public SupplementPipeline(){}

	@Override
	public void doProcess(ResultItems resultItems, Task task)
	{
		if(spiderInfo == null){
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		}
		
		//数据入库暂时根据接口传来的值判断需要补充哪些数据
		TVRecordModel model = resultItems.get(TVRecordModel.class.getName());// 评分、播放量、赞、踩
		if (model != null)
		{
			supplementService.saveTvRecord(model);
			if(StringUtil.isNotEmpty(model.getPlayCount())){
				Map paramap = new HashMap();
				List<Map> recordList =supplementService.getYeaterdayTvRecord(model);
				if(null != recordList&&recordList.size()>0){
					Map map = recordList.get(0);
					if(null != map.get("dayplaycount")){
						long yesterdayplaycount = Long.valueOf(map.get("playcount").toString());
						long newDayPlayCount = Long.valueOf(model.getPlayCount());
						paramap.put("tvId", model.getTvId());
						paramap.put("tvName", model.getTvName());
						paramap.put("resourceSiteId", model.getResourceSiteId());
						paramap.put("playCount", model.getPlayCount());
						paramap.put("dayPlayCount", newDayPlayCount-yesterdayplaycount+"");
					}else{
						paramap.put("tvId", model.getTvId());
						paramap.put("tvName", model.getTvName());
						paramap.put("resourceSiteId", model.getResourceSiteId());
						paramap.put("playCount", model.getPlayCount());
						paramap.put("dayPlayCount", "0");
					}
				}else{
					paramap.put("tvId", model.getTvId());
					paramap.put("tvName", model.getTvName());
					paramap.put("resourceSiteId", model.getResourceSiteId());
					paramap.put("playCount", model.getPlayCount());
					paramap.put("dayPlayCount", "0");
				}
				supplementService.saveTvRecordDay(paramap);
			}
			
		}
	
		List<TVPubCommentModel> commentList = resultItems
				.get(TVPubCommentModel.class.getName());// 评论内容
		String key = DataRedisKeyUtils
				.getKey(Constants.DataRedisKey.TV_Content_YYYYMMDD);
		if (commentList != null && !commentList.isEmpty())
		{
			for (TVPubCommentModel pubComment : commentList){
				cacheHandler.addListCache(key, pubComment);
			cacheHandler.setCacheTimeout(key);
			}
		}
		
	}

	public void setSpiderInfo(JSONObject spiderInfo) {
		this.spiderInfo = spiderInfo;
	}

}
